<?php
class Off_m extends MY_Model
{
	
	//public $year = date('Y');
	
	public $type_off_data = array(
		0	=>	'Về sớm',
		1	=>	'Đi trễ',
		2	=>	'Nghỉ theo giờ',
		3	=>	'Nghỉ theo ngày'
	);
	function __construct(){
		parent::__construct();
		$this->_table = 'em_off_'.date('Y');
		//echo SITE_REF.'_'.$this->_table;exit;
		//tao bang cho nam hien tai
		if (!$this->db->table_exists($this->_table))
		{
//			echo 1;exit;
		   $this->create_table($this->_table);
		}
	}
	
	function create_table( $table ){
		//SITE_REF
		$query = 'CREATE TABLE `'.$table.'` (
		  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
		  `title` varchar(255) DEFAULT NULL COMMENT \'ly do nghi\',
		  `user_id` int(11) NOT NULL,
		  `time_start` int(11) NOT NULL,
		  `time_end` int(11) DEFAULT NULL,
		  `type_off` int(1) DEFAULT NULL COMMENT \'0: ve som, 1: đi trễ ,2: nghi theo giờ, 2: nghi theo ngày\',
		  `month` int(2) NOT NULL,
		  `notes` varchar(255) DEFAULT NULL,
		  `salary` smallint(1) NOT NULL COMMENT \'0: Nghỉ không lương , 1:Nghỉ có lương\',
		  `status` smallint(1) NOT NULL DEFAULT \'0\' COMMENT \'0: chưa duyệt, 1: đã duyêt , 2: ko duyệt\',
		  `value` float NOT NULL DEFAULT \'0\' COMMENT \'quy đổi ra số phút - Nếu về sớm/đi trễ 1 tiếng thì value: 60; nếu nghi 1 ngay thi 480\',
		  `room_id` int(11) DEFAULT NULL,
		  `daycreated` int(11) DEFAULT NULL,
		  `position_id` int(11) DEFAULT NULL,
		  `important` tinyint(1) DEFAULT 0 COMMENT \'giám đốc duyệt\',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8';
		$this->db->query($query);
		
	}
	
	function add( $data ){
		$this->load->model("erp/dayoff_m");//model ngay nghi cua nam
		$list_nghi =  $this->dayoff_m->list_short();
		//print_r($list_nghi);exit;
		$this->_table = 'em_off_'.date('Y');
		//quy doi value
		//nghi theo gio , nghi theo ngay
		$value_nghi = 0;
		if($data['type_off'] == 2 || $data['type_off'] == 3 ){
			
			//loai chu nhat
			if($data['type_off'] == 3)
			{
				$time_start	 = date('m/d/Y',$data['time_start']);
				$data_time_start = strtotime($time_start);
				//tinh toan neu xin nghi PM thi + them 1440
				$time_end	 = date('m/d/Y',$data['time_end']);
				$data_time_end = strtotime($time_end);
			}
			if(!empty($data_time_end)){
				for($i = $data_time_start ; $i < $data_time_end; $i+=1440){
					
					$check = $this->unix_to_gmt_sevent($i);
					if($check == 7){
						$value_nghi += 1440;
					}
					else{
						//kiem tra ngay nghi cua nam
						foreach($list_nghi as $ng){
							$v = date("m-d",$i);
							
							if($v==$ng->value){
								$value_nghi += 1440;
							}
						}
					}
				}
			}
			if($data['time_end'] != NULL){
				$data['value'] = ( $data['time_end'] - $data['time_start'] - $value_nghi ) / 60;
			}
			//xu ly nghi ngay
			$tmp =  $data['value'] / 60 / 24;//so ngay
			//nghi theo ngay
			if($tmp >= 1 ){ //480 la so phut 1 ngay lam
				$data['value'] = $tmp * 480;
			}
		}
		if($data['value'] > 480){//lon hon 1 ngay
			$data['important'] = 1;
		}
		if($data['type_off'] == 3){
			$data['value'] = $data['total_day'] * 480;//1 ngay 480
		}
		unset($data['total_day']);
		$data['daycreated'] = strtotime(date("Y-m-d H:i"));
		$data['salary']  = 1;
		//
		parent::insert($data);
	}
	
	//ca nhan
	/*function get_status_02( $user_id, $in_month = false){
		//$w = '(status = 0 or status = 2)';
		//$this->db->where($w);
		$this->db->where('user_id',$user_id);
		if($in_month==true)
			$this->db->where('month',date('m'));
		$this->db->order_by('time_start');
		return parent::get_all();
	}
	*/
	//truong phong xem don nghi cua nhan vien de duyet
	/*
	function get_status_0( $user_id, $room_id, $in_month = false,$tp = 4 ){
		$this->db->select('o.*, p.full_name');
		$this->db->from('em_profile p');
		$this->db->join($this->_table.' o', 'p.user_id = o.user_id');
		//$this->db->where_not_in('o.user_id',  array($user_id));
		//
		$this->db->where('o.status',0);
		$this->db->where('o.position_id',$tp);
		if($tp == 4)
			$this->db->where('o.room_id',$room_id);
		//if($tp == 2)
		//	$this->db->where('o.room_id',$room_id);
		$this->db->where_not_in('o.user_id',  array($user_id));
		if($in_month==true)
			$this->db->where('o.month',date('m'));
		$this->db->order_by('o.time_start');
		//$this->db->get()->result();echo $this->db->last_query();exit;
		 return $this->db->get()->result();//return
	}	
	*/
	//list da duyet
	
	/*function get_status_1( $room_id, $in_month = false ){
		$this->db->where('status',1);
		$this->db->where('room_id',$room_id);
		if($in_month==true)
			$this->db->where('month',date('m'));
		$this->db->order_by('time_start');
		return parent::get_all();
	}	
	*/
	function delete_xinnghi( $id ){
		$this->db->where('id', $id);
		//$this->delete();
		parent::delete($id);
	}
	
	function type_off_name( $id ){
		$result = "";
		$result = $this -> type_off_data[$id];
		return $result;
	}
	function duyet( $id, $salary, $status = 1 ){
		$this->db->where('id',$id);
		$this->db->update($this->_table,array('salary'=>$salary,'status'=>$status));
	}
	//loai bo chu nhat
	function unix_to_gmt_sevent($day) 
 	{
  		$str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
  		$str_out = array ( 1, 2, 3, 4, 5, 6, 7);
  		$time  = gmdate("D", $day+7*3600);
  		$time  = str_replace($str_in, $str_out, $time);
  		return $time;
 	}
	
	//
	public function get_many_by($params = array(),$page_size = 10){
		$this->_table = $this->_table.' o';
		$this->get_many_by_query($params);
		$total_row = $this->db->count_all_results($this->_table);
		
		
		$this->get_many_by_query($params);
		if ( ! empty($params['limit']))
		{
			 $this->db->limit($page_size,$params['limit'] * $page_size);	
		}
		else
			$this->db->limit($page_size);	
			
		$model = new stdClass();
		$model->result = parent::get_all();//$this->db->get()->result();
		$model->total_row = $total_row;
		return $model;
	}
	private function get_many_by_query($params = array()){
		//$this->time_zone();
		$this->db->select('o.*, u.display_name');
		//$this->db->from($this->_table.' o');
		$this->db->join('ser_users u', 'u.id = o.user_id');
		if(!empty($params['status'])){
			$this->db->where('o.status',$params['status']);
		}
		else{
			if(isset($params['status'])){
				if($params['status'] == 0)
					$this->db->where('o.status',$params['status']);
			}
		}
		if(!empty($params['room_id'])){
			$this->db->where('o.room_id',$params['room_id']);
		}
		if(!empty($params['boss_status'])){
			$this->db->where('o.boss_status',$params['boss_status']);
		}
		if(!empty($params['current_user'])){
			$this->db->where_not_in('o.user_id',  array($params['current_user']));
		}
		if(!empty($params['user_id'])){
			$this->db->where('o.user_id',  $params['user_id']);
		}
		
		if(!empty($params['in_month'])){
			$this->db->where('o.month',date('m'));
		}
		if(!empty($params['type_off'])){
			$this->db->where('o.type_off',$params['type_off']);
		}
		else{
			if(isset($params['type_off'])){
				if($params['type_off'] == 0)
					$this->db->where('o.type_off',$params['type_off']);
			}
		}
		if(!empty($params['important'])){
			$this->db->where('o.important',$params['important']);
		}
		if(!empty($params['month'])){
			$this->db->where('o.month',$params['month']);
		}
		if(!empty($params['year'])){
			$this->_table =  'em_off_'.$params['year'].' o';
		}
		$this->db->order_by('o.id');
		//$this->db->order_by('o.time_start');
	}
	
	public function status($id,$status,$salary = NULL){
		//neu important = 1 thi status = 0 va doi giam doc duyet boss_status = 2
		$array = array();
		$this->db->where('id',$id);
		$off = parent::get($id);
		if($off->important == 1){
			$array['boss_status'] = 2;
			$array['status'] = 0;
		}
		else{
			$array['status'] = $status;
		}
		//giam doc
		if($off->important == 1 && $off->boss_status == 2){
			$array['status'] = $status;
		}
		//echo $status;exit;
		//print_r($array);exit;
		if($salary == true){//khong luong
			$array['salary'] = 0;
		}
		parent::update($id,$array);
	}
	
}